import db from '../../db.js';
import Router from 'koa-router';
import response from '../../response.js';
import arrayToTree from '../../utils/arrayToTree.js';
import utils from '../../utils/index.js';
import _ from 'lodash';
const router = new Router();
router.post('/', async (ctx) => {
  const {
    name,
    type,
    code = '',
    urls = '',
    router = '',
    permission = '',
    parentId,
    hide = 2,
    sort = 0,
  } = ctx.data || {};
  if (parentId === '' || parentId === null || parentId === undefined) {
    ctx.body = response.fail({ msg: '父节点不能为空' });
    return;
  }
  const [hasRows] = await db.execute('select id from menu where id=? limit 1', [parentId]);

  if (!hasRows?.length) {
    ctx.body = response.fail({ msg: '父节点不存在' });
    return;
  }

  if (!name && name !== 0) {
    ctx.body = response.fail({ msg: '名称不能为空' });
    return;
  }
  if (!type && type !== 0) {
    ctx.body = response.fail({ msg: '类型不能为空' });
    return;
  }
  if (!permission && permission !== 0) {
    ctx.body = response.fail({ msg: '权限标识不能为空' });
    return;
  }
  const createTime = new Date().getTime();
  const values = [
    name,
    type,
    code,
    urls,
    router,
    parentId,
    permission,
    hide,
    createTime,
    createTime,
    sort,
  ];
  const [result] = await db.query(
    'INSERT INTO menu(name,type,code,urls,router,parentId,permission,hide,create_time,update_time,sort)  values(?, ?, ?, ?, ?, ?,?, ?,?, ?,?)',
    values,
  );
  if (result?.affectedRows > 0) {
    ctx.body = response.success({
      msg: '新增成功',
    });
  } else {
    ctx.body = response.fail({
      msg: '新增失败',
    });
  }
});
router.get('/', async (ctx) => {
  const [rows] = await db.query(
    'select id,name,type,sort,hide,code,urls,router,permission,parentId,create_time as createTime,update_time as updateTime from `menu` where id!=0 and  isDelete=0 ORDER BY sort ASC',
  );
  ctx.body = response.success({
    data: {
      list: rows,
    },
  });
});
router.delete('/:id', async (ctx) => {
  const { id } = ctx.params || {};
  if (_.isNil(id)) {
    ctx.body = response.fail({ msg: 'id不能为空' });
    return;
  }
  const [hasRows] = await db.execute('select id from menu where id=? limit 1', [id]);

  if (!hasRows?.length) {
    ctx.body = response.fail({ msg: '数据不存在' });
    return;
  }
  const [result] = await db.query('update   `menu` set isDelete=1 where id=?', id);
  if (result?.affectedRows > 0) {
    ctx.body = response.success({ msg: '删除成功' });
  } else {
    ctx.body = response.fail({ msg: '删除失败' });
  }
});

router.put('/', async (ctx) => {
  const {
    id,
    name,
    type,
    code,
    urls,
    router,
    permission,
    hide,
    parentId,
    sort = 0,
  } = ctx?.data || {};
  if (id === '' || id === null || id === undefined) {
    ctx.body = response.fail({ msg: 'id不能为空' });
    return;
  }
  const [hasRows] = await db.query('select id from menu where id=? limit 1', [id]);
  if (!hasRows?.length) {
    ctx.body = response.fail({ msg: 'menu点不存在' });
    return;
  }
  const update_time = new Date().getTime();
  const saveData = utils.filterObjectNill({
    name,
    type,
    code,
    urls,
    router,
    parentId,
    permission,
    hide,
    update_time,
    sort,
  });
  const [result] = await db.query('update menu set? where id=?', [saveData, id]);

  if (result?.affectedRows > 0) {
    ctx.body = response.success({ msg: '修改成功' });
  } else {
    ctx.body = response.fail({ msg: '修改失败' });
  }
});
router.get('/list', async (ctx) => {
  const [rows] = await db.query(
    'select id,name,type,sort,hide,code,urls,router,permission,parentId,create_time as createTime,update_time as updateTime from `menu` where id!=0 and type!=3 and isDelete=0 ORDER BY sort ASC',
  );
  ctx.body = response.success({
    data: rows,
  });
});
router.get('/tree', async (ctx) => {
  const [rows] = await db.query(
    'select id,name,type,code,hide,urls,sort,router,permission,parentId,create_time as createTime,update_time as updateTime from `menu` where  isDelete=0 ORDER BY sort ASC',
  );
  ctx.body = response.success({
    data: arrayToTree(rows, null),
  });
});
export default router;
